import config from './config';


export default (url, data = {}, method = 'GET') => {
    return new Promise((resolve,reject) => {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.request({
            url: config.host + url,
            data,
            method,
            header:{
                cookie: wx.getStorageSync('cookies') ? wx.getStorageSync('cookies').find(v => v.startsWith('MUSIC_U')) : ''
            },
            success: (res)=>{
                if(data.isLogin){
                    // 判断是否登录请求，请求成功存入cookie
                    // 异步本地存储
                    wx.setStorage({
                        key: 'cookies',
                        data: res.cookies,
                    });
                }
                resolve(res.data);
            },
            fail: (err)=>{
                console.log('请求失败：',err);
                wx.showToast({
                    title: '请求失败',
                    icon: 'none'
                });
                reject(err);
            },
            complete: () => {
                wx.hideLoading();
            }
        });
    });
}